package it.datagraf.formazione.hellostruts.struts;

import java.util.HashSet;
import it.datagraf.formazione.hellostruts.entity.Competenza;
import it.datagraf.formazione.hellostruts.entity.CurriculumVitae;
import it.datagraf.formazione.hellostruts.form.BeanCurriculumForm;
import it.datagraf.formazione.hellostruts.service.CVServiceInteface;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class OwnerInsertCvAction extends Action {

	public static Log log = LogFactory.getLog(OwnerInsertCvAction.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		BeanCurriculumForm ef = (BeanCurriculumForm) form;
		CurriculumVitae cv = ef.getCurriculumVitae();
		log.info("Sono in OwnerEditCvAction - salvo nel db il curriculum di "+ ef.getCurriculumVitae().getNome()+" "+ef.getCurriculumVitae().getCognome());
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		CVServiceInteface serv= (CVServiceInteface) ctx.getBean("CVservice");
		
		cv.setNome(cv.getNome());
		cv.setCognome(cv.getCognome());
		cv.setSesso(cv.getSesso());
		cv.setNote(cv.getNote());
		cv.setFile(cv.getFile());
		cv.setEta(cv.getEta());
		cv.setOwner(request.getRemoteUser());		
		
//		Competenza c=serv.getCompetenza(new Long(1));
//		HashSet<Competenza> cs=new HashSet<Competenza>();
//		cs.add(c);
//		cv.setCompetenze(cs);
		
		serv.insertCV(cv);
		
		
		request.setAttribute("cv", cv);
		return mapping.findForward("success");
	}
}